package com.xsy.de.restful.controller;

import com.xsy.de.entity.IUser;
import com.xsy.de.service.impl.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping(value = "/v1/user")
@Api(tags = "Neo4j版用户操作接口")
public class IUserController {

    private final IUserService userService;

    public IUserController(IUserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "添加用户", notes = "用户名和技能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "skill", value = "技能名称", required = false, paramType = "query", dataType = "String"),

    })
    @PostMapping(value = "/addUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public boolean addUser(String name, String skill) {
        try {
            IUser result = userService.addUser(new IUser(name, skill));
            if (result.getId() != null) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }

    }

    @ApiOperation(value="添加用户关系", notes="用户名和关系用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "srcName", value = "用户姓名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "tarName", value = "关系用户姓名", required = true, paramType = "query", dataType = "String"),

    })
    @PostMapping(value = "/addRelationUser", produces= MediaType.APPLICATION_JSON_UTF8_VALUE)
    public boolean addRelationUser(String srcName, String tarName){
        try {
            IUser srcUser = userService.findByName(srcName);
            IUser tarUser = userService.findByName(tarName);
            if (srcUser != null && tarUser !=null){
                srcUser.addKnowUser(tarUser);
                userService.addUser(srcUser);
                return true;
            }
            return false;
        }catch (Exception e){
            return false;
        }
    }

    @ApiOperation(value="查找用户资料", notes="根据用户姓名查找用户信息")
    @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public IUser findByName(@PathVariable("name") String name){
        try {
            return userService.findByName(name);
        }catch (Exception e){
            return null;
        }
    }

    @ApiOperation(value="查看所有用户信息", notes="查看所有用户信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<IUser> findAllUserInfo(){
        try {
            return userService.findAllUser();
        }catch (Exception e){
            return null;
        }
    }

    @ApiOperation(value="查询用户技能相关人员", notes="根据用户名和技能名称查找最小层级的关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "skill", value = "技能名称", required = true, paramType = "query", dataType = "String"),

    })
    @PostMapping(value = "/findSkillUser", produces= MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<String> findSkillUser(String name, String skill){
        if (StringUtils.isBlank(name) || StringUtils.isBlank(skill)){
            return null;
        }
        try {
            List<IUser> users = userService.findUserSkill(name,skill);
            if (users.size() <= 0){
                return null;
            }
            List<String> result = new ArrayList<>();
            for (IUser user:users){
                result.add(user.getName());
            }
            return result;
        }catch (Exception e){
            return null;
        }



    }
}
